package com.example.xymarket.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import com.example.xymarket.myView.StatusBar;
import com.example.xymarket.R;
import com.example.xymarket.httpUtil.*;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class LoginActivity extends AppCompatActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        check();
        setContentView(R.layout.activity_login);
        username=(EditText)findViewById(R.id.username);
        StatusBar.setStatusBarColor(this,StatusBar.white);
    }

    /**
     * 申请内存权限
     */
    public void check()
    {
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED ||
                ContextCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED ||
                ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED ||
                ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED)
        {
            ActivityCompat.requestPermissions(this, new String[]
                    {
                            Manifest.permission.CAMERA,
                            Manifest.permission.RECORD_AUDIO,
                            Manifest.permission.WRITE_EXTERNAL_STORAGE,
                            Manifest.permission.READ_EXTERNAL_STORAGE}, 100);

        }
    }

    EditText username;

    public void Login(View view) throws InterruptedException {
        EditText password=(EditText)findViewById(R.id.password);
        UserInfoService.check(username.getText().toString(),password.getText().toString());
        while(UserInfoService.isLogin==0)
        {
            Thread.sleep(100);
        }
        if(UserInfoService.isLogin==-1)
        {
            Toast.makeText(this,"登陆失败请检查账号密码！", Toast.LENGTH_LONG).show();
            UserInfoService.isLogin=0;
        }
        else
        {
            Toast.makeText(this,"登陆成功！", Toast.LENGTH_LONG).show();
            Intent intent = new Intent(LoginActivity.this, HomeActivity.class);
            startActivity(intent);overridePendingTransition(0, 0);
        }
    }
}